package com.neusoft.controller;

import java.util.List;

import com.neusoft.po.User;
import com.neusoft.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Orders;
import com.neusoft.service.OrdersService;

//@CrossOrigin("*")
@RefreshScope
@RestController
@RequestMapping("/OrdersController")
public class OrdersController {

	@Autowired
	private OrdersService ordersService;

	// 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
	// 并获取自动生成的订单编号，
	// 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
	// 然后根据用户编号、商家编号删除购物车表中的数据。

	@PostMapping("/createOrders/{userId}/{businessId}/{daId}/{orderTotal}")
	public CommonResult<Integer> createOrders(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") Integer businessId, 
			@PathVariable("daId") Integer daId,
			@PathVariable("orderTotal") Integer orderTotal) throws Exception {
		System.out.println("Using orders_server_10600");
		Orders orders = new Orders();
		orders.setUserId(userId);
		orders.setBusinessId(businessId);
		orders.setDaId(daId);
		orders.setOrderTotal(orderTotal);
		int orderId = ordersService.createOrders(orders);
		System.out.println(CommonUtil.getCurrentDate() + " 用户(" + orders.getUserId() + ") 创建订单 编号："
				+ orders.getOrderId() + "  金额：" + orders.getOrderTotal());
		return new CommonResult(200, "success（10600）", orderId);
	}

	// 根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
	@GetMapping("/getOrdersById/{orderId}")
	public CommonResult<Orders> getOrdersById(
			@PathVariable("orderId") Integer orderId) throws Exception {
		System.out.println("Using orders_server_10600");
		Orders orders = ordersService.getOrdersById(orderId);
		return new CommonResult(200, "success（10600）", orders);
	}

	// 根据用户编号查询此用户的所有订单信息
	@GetMapping("/listOrdersByUserId/{userId}")
	public CommonResult<List> listOrdersByUserId(
			@PathVariable("userId") String userId) throws Exception {
		System.out.println("Using orders_server_10600");
		List<Orders> list = ordersService.listOrdersByUserId(userId);
		return new CommonResult(200, "success（10600）", list);
	}

	// 支付订单，并扣除和新增相应数量的积分
	/*
	 * @RequestMapping("/payOrders") public int payOrders(Integer orderId,Integer
	 * usedScore) throws Exception { Orders orders=new Orders(); orders =
	 * ordersService.getOrdersById(orderId);
	 * System.out.println(CommonUtil.getCurrentDate()+" 用户("+orders.getUserId()
	 * +") 支付订单 编号："+orders.getOrderId()+"  金额："+orders.getOrderTotal() +" 使用积分: "
	 * +usedScore); return ordersService.payOrders(orderId,usedScore); }
	 */

	@PostMapping("/payOrders/{orderId}/{usedScore}")
	public CommonResult<Integer> payOrders(
			@PathVariable("orderId") Integer orderId,
			@PathVariable("usedScore") Integer usedScore) throws Exception {
		System.out.println("Using orders_server_10600");
		Orders orders = ordersService.getOrdersById(orderId);
		System.out.println(CommonUtil.getCurrentDate() + " 用户(" + orders.getUserId() + ") 支付订单 编号："
				+ orders.getOrderId() + "  金额：" + orders.getOrderTotal() + " 使用积分: " + usedScore);
		int result = ordersService.payOrders(orderId, usedScore);
		return new CommonResult<>(200, "success（10600）", result);
	}
}
